<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.recommender"
        clearable
        class="filter-item"
        style="width: 200px"
        placeholder="请输入推荐人账号"
      />
      <el-input
        v-model="listQuery.username"
        clearable
        class="filter-item"
        style="width: 200px"
        placeholder="请输入用户名"
      />
      <el-input
        v-model="listQuery.userId"
        clearable
        class="filter-item"
        style="width: 200px"
        placeholder="请输入用户Id"
      />
      <el-date-picker
        v-model="listQuery.timeArray"
        type="datetimerange"
        value-format="yyyy-MM-dd HH:mm:ss"
        class="filter-item"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions"
      />

      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >查找</el-button
      >
      <el-button
        :loading="downloadLoading"
        class="filter-item"
        type="primary"
        icon="el-icon-download"
        @click="handleDownload"
        >导出</el-button
      >
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      :summary-method="getSummaries"
      show-summary
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        align="center"
        width="100px"
        label="用户ID"
        prop="id"
        sortable
      />
      <el-table-column align="center" label="用户名" prop="nickname" />
      <!-- <el-table-column align="center" label="FSP" prop="fsp"></el-table-column>
      <el-table-column align="center" label="FSPT" prop="fspt" />
      <el-table-column align="center" label="SUN" prop="sun" />
      <el-table-column align="center" label="SUND" prop="sund" />
      <el-table-column align="center" label="奖励金" prop="jianglijin" /> -->
      <el-table-column align="center" label="积分" prop="xiaofeiquan" />
      <el-table-column align="center" label="个人业绩" prop="" />
      <el-table-column align="center" label="总业绩" prop="" />
      <el-table-column align="center" label="团队总数" prop="" />
      <!-- <el-table-column align="center" label="用户等级" prop="userLevel">
        <template slot-scope="scope">
          <el-tag>{{ levelDic[scope.row.userLevel] }}</el-tag>
        </template>
      </el-table-column> -->

      <el-table-column align="center" label="代理级别" prop="serveCenter">
        <template slot-scope="scope">
          <el-tag>{{ levelDic[scope.row.serveCenter] }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag>{{ statusDic[scope.row.status] }}</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchList, userDetail } from "@/api/user";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination

export default {
  name: "Performance",
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        username: undefined,
        recommender: undefined,
        mobile: undefined,
        userId: undefined,
        timeArray: [],
        sort: "add_time",
        order: "desc",
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      downloadLoading: false,
      levelDic: ["会员", "省代", "市代", "县代"],
      statusDic: ["可用", "禁用", "注销"],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取数据
    getList() {
      this.listLoading = true;
      if (this.listQuery.timeArray && this.listQuery.timeArray.length === 2) {
        this.listQuery.start = this.listQuery.timeArray[0];
        this.listQuery.end = this.listQuery.timeArray[1];
      } else {
        this.listQuery.start = null;
        this.listQuery.end = null;
      }
      if (this.listQuery.userId) {
        userDetail(this.listQuery.userId)
          .then((response) => {
            this.list = [];
            if (response.data.data) {
              this.list.push(response.data.data);
              this.total = 1;
              this.listLoading = false;
            } else {
              this.list = [];
              this.total = 0;
              this.listLoading = false;
            }
          })
          .catch(() => {
            this.list = [];
            this.total = 0;
            this.listLoading = false;
          });
      } else {
        const { ...data } = this.listQuery;
        delete data["timeArray"];
        fetchList(data)
          .then((response) => {
            this.list = response.data.data.list;
            console.log(this.list);
            this.total = response.data.data.total;
            this.listLoading = false;
          })
          .catch(() => {
            this.list = [];
            this.total = 0;
            this.listLoading = false;
          });
      }
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then((excel) => {
        const tHeader = ["用户名", "团队总数", "总业绩", "个人业绩"];
        const filterVal = ["username", "mobile", "gender", "birthday"];
        excel.export_json_to_excel2(tHeader, this.list, filterVal, "业绩查询");
        this.downloadLoading = false;
      });
    },
    // 总和统计
    getSummaries() {
      return ["总和"];
    },
  },
};
</script>

<style>
.serveCheckBox {
  zoom: 200%;
}
</style>
